package com.yuhang.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yuhang.entity.HyBill;
import com.yuhang.entity.HyBuild;
import com.yuhang.entity.HyRepair;
import com.yuhang.entity.HyUser;
import com.yuhang.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yuhang
 * @since 2021-04-05
 */
@RestController
@RequestMapping("/hy-user")
public class HyUserController {

    @Value("${imgPath}")//获取springboot配置文件中的变量,把配置文件中的变量赋值给该属性
    private String path;
    @Autowired
    private HyUserService hyUserService;
    @Autowired
    private HyBuildService hyBuildService;
    @Autowired
    private HyAdminService hyAdminService;
    @Autowired
    private HyBillService hyBillService;

    @RequestMapping("/showUser")
    public Map<String, Object> showUser(@RequestParam(value = "page",defaultValue = "1")int pageNum,
                                        @RequestParam(value = "limit",defaultValue = "5")int pageSize,
                                        @RequestParam(value = "likename",defaultValue = "")String likename,
                                        @RequestParam(value = "liketelephone",defaultValue = "")String liketelephone,
                                        @RequestParam(value = "buildId",defaultValue = "")Integer buildId){
        List<HyUser> page = hyUserService.selectUserPage(pageNum, pageSize, likename, liketelephone, buildId);
        List<HyUser> list = hyUserService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",page);
        return map;
    }
    @RequestMapping("/addUser")
    public Map<String,Object> addUser(HyUser hyUser){
        QueryWrapper<HyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("build_id",hyUser.getBuildId()).eq("huhao",hyUser.getHuhao());
        HyUser one = hyUserService.getOne(queryWrapper);
        if(Objects.isNull(one)){
            if(hyAdminService.selectByUsername(hyUser.getUsername())){
                HyBuild hyBuild = hyBuildService.getById(hyUser.getBuildId());
                hyUser.setBuildName(hyBuild.getName());
                hyUserService.save(hyUser);
                Map<String, Object> map = new HashMap<>();
                map.put("code",0);
                return map;
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("code",2);
                map.put("message","该用户名已存在！！！");
                return map;
            }
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该户已经存在住户,请选择其他房间！！！");
            return map;
        }


    }

    @RequestMapping("/deleteUser")
    public Map<String, Object> deleteUser(@NotEmpty Integer id) {
        QueryWrapper<HyBill> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",id).eq("state",0);
        List<HyBill> list = hyBillService.list(queryWrapper);
        if(list.size()>0){
            Map<String, Object> map = new HashMap<>();
            map.put("code", 1);
            map.put("message","该用户还有未结清的账单！！！");
            return map;
        }else {
            hyUserService.removeById(id);
            hyUserService.deleteCarAndPetByUserId(id);
            Map<String, Object> map = new HashMap<>();
            map.put("code", 0);
            return map;
        }
    }
    @RequestMapping("/updateUser")
    public Map<String,Object> updateUser(HyUser hyUser){
        QueryWrapper<HyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("build_id",hyUser.getBuildId()).eq("huhao",hyUser.getHuhao());
        HyUser one = hyUserService.getOne(queryWrapper);
        if(Objects.isNull(one)){

            String username = hyUser.getUsername();
            Boolean aBoolean1 = hyAdminService.selectByAdmin(username);
            Boolean aBoolean2 = hyAdminService.selectByRepair(username);
            HyUser hyUser1 = hyUserService.getById(hyUser.getId());
            QueryWrapper<HyUser> qw = new QueryWrapper<>();
            qw.eq("username",username);
            List<HyUser> list = hyUserService.list(qw);
            Boolean aBoolean3 = false;
            if(list.size()>1){
                aBoolean3=false;
            }
            if(list.size()==1){
                HyUser hyUser2 = list.get(0);
                if(hyUser2.getId()==hyUser1.getId()){
                    aBoolean3=true;
                }
            }
            if(aBoolean1&&aBoolean2&&aBoolean3){
                hyUserService.updateById(hyUser);
                Map<String, Object> map = new HashMap<>();
                map.put("code",0);
                return map;
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("code",2);
                map.put("message","该用户名已存在！！！");
                return map;
            }
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该户已经存在住户,请选择其他房间！！！");
            return map;
        }
    }
    @RequestMapping("/updateUser2")
    public Map<String,Object> updateUser2(HyUser hyUser, String newpassword, HttpSession session){
        QueryWrapper<HyUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("build_id",hyUser.getBuildId()).eq("huhao",hyUser.getHuhao());
        HyUser one = hyUserService.getOne(queryWrapper);
        if(Objects.isNull(one)){
            String username = hyUser.getUsername();
            Boolean aBoolean1 = hyAdminService.selectByRepair(username);
            Boolean aBoolean2 = hyAdminService.selectByAdmin(username);
            HyUser hyUser1 = hyUserService.getById(hyUser.getId());
            QueryWrapper<HyUser> qw = new QueryWrapper<>();
            qw.eq("username",username);
            List<HyUser> list = hyUserService.list(qw);
            Boolean aBoolean3 = true;
            if(list.size()>1){
                aBoolean3=false;
            }
            if(list.size()==1){
                HyUser hyUser2 = list.get(0);
                if(hyUser2.getId()==hyUser.getId()){
                    aBoolean3=true;
                }else {
                    aBoolean3=false;
                }
            }
            if(aBoolean1&&aBoolean2&&aBoolean3){
                if(hyUser1.getPassword().equals(hyUser.getPassword())){
                    if(newpassword!=null&&newpassword!=""){
                        hyUser.setPassword(newpassword);
                    }
                    hyUserService.updateById(hyUser);
                    session.setAttribute("hyUser",hyUserService.getById(hyUser));
                    Map<String, Object> map = new HashMap<>();
                    map.put("code",0);
                    return map;
                }else {
                    Map<String, Object> map = new HashMap<>();
                    map.put("code",2);
                    map.put("message","密码错误！！！");
                    return map;
                }
            }else {
                Map<String, Object> map = new HashMap<>();
                map.put("code",3);
                map.put("message","该用户名已存在！！！");
                return map;
            }
        }else{
            Map<String, Object> map = new HashMap<>();
            map.put("code",1);
            map.put("message","该户已经存在住户,请选择其他房间！！！");
            return map;
        }
    }
    @RequestMapping("/uploadImage")
    public Map<String,Object> uploadHeadImg(MultipartFile file) throws IOException {//收参使用file
        file.transferTo(new File(path,file.getOriginalFilename()));
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg",file.getOriginalFilename());
        return map;
    }


}
